<?php

namespace Partner\Controller;

use Utuo\BackgroundController;

class AdminController extends BackgroundController {
	private $isSuper;
	// 初始化
	public function _initialize() {
		$this->setControllerTitle('管理员管理');
		$this->setActionTitle(array(
				'index' => '管理员列表',
				'add' => '添加管理员',
				'modify' => '修改管理员',
				'recycle' => '回收站',
				'setRole'=>'权限配置'
		));
		$this->setActionNav(array(
				'index',
				'add',
				'recycle'
		));
		
		$this->isSuper=M('PartnerAdmin')->getFieldById($this->adminId,'is_super');
		$rbacPartner = C('RBAC_PARTNER');
		$this->tableNames = \Utuo\Rbac::getAuthModelNames( array('role' => $rbacPartner['RBAC_ROLE_TABLE'], 'user' => $rbacPartner['RBAC_USER_TABLE'], 'access' => $rbacPartner['RBAC_ACCESS_TABLE'], 'node' => $rbacPartner['RBAC_NODE_TABLE']) );
	    
	}
	
	// 管理员列表
	public function indexAction() {
	    $this->getAdminLists(0);
	}
	
	// 管理员回收站
	public function recycleAction() {
		$this->getAdminLists(1);
	}
	
	// 添加管理员
	public function addAction(){
		$this->isSuper or $this->error('您没有权限');
		$this->display();
	}
	
	// 获取列表
	private function getAdminLists($is_deleted) {
		$condition = array(
			'is_deleted' => $is_deleted,
		
		);
		
	    $this->isSuper?$condition['pid']=$this->aid:$condition['id']=$this->adminId;
		$page = $this->getPage();
		$pagesize = $this->getPageSize();
		$partnerAdminLists = M('PartnerAdmin')->where($condition)->limit($pagesize)->page($page)->order('is_super desc')->select();
		
		$count = M('PartnerAdmin')->where($condition)->count();
		$this->assign('partnerAdminLists', $partnerAdminLists);
		$this->showPage($count, $pagesize);
		$this->display();
	}
	
	// 提交添加
	public function addPostAction() {
		$d = D('PartnerAdmin');
		if ($d->create()) {
			$d->pid = $this->aid;
			if ($d->add()) {
				$this->ajaxSuccess('添加成功');
			} else {
				$this->ajaxError('添加失败');
			}
		} else {
			$this->ajaxError('创建数据失败');
		}
	}
	
	// 修改
	public function modifyAction() {
		if ( !$this->isSuper and ($id = $this->getId()) != $this->adminId ) {
			$this->error('无权修改。');
		}
		$admin = D("PartnerAdmin")->getOneById($this->getId());
		$this->assign('partnerAdmin', $admin);
		$this->display();
	}
	
	// 提交修改
	public function modifyPostAction() {
		$m = D('PartnerAdmin');
		if( $m->create() ) {
			if ( ($id = $this->getId()) != session('aid') ) {
				$this->ajaxError('无权修改。');
			}
			if(false !== $m->save()){
				$this->ajaxSuccess("保存成功");
			}else{
				$this->ajaxError();
			}
		}else{
			$this->ajaxError($m->getError());
		}
	}
	public function isRoleAction($cid, $isrole) {
		M('PartnerAdmin')->where('id=%d',$cid)->setField("is_role",$isrole) ? $this->ajaxSuccess():$this->ajaxError();
	}
	// 分配角色
	public function setRoleAction( $admin_cid ) {
		$pid = M("PartnerAdmin")->where("id={$admin_cid}")->getField("pid"); // 客户id
		$roles = M($this->tableNames['role'][1])->where("(pid={$pid}) and is_deleted=%d", 0)->getField('id,name'); //合作商与客户的角色
	    $curRoles = M($this->tableNames['user'][1])->where('admin_cid=%d',$admin_cid)->getField("role_id",true); // 当前用户已分配的角色
		$data['roleCheckbox']=\Utuo\Form::makeCheckbox($roles, $curRoles, 'role_id[]');
		$data['adminCid'] = $admin_cid;
		$this->assign("data",$data);
		$this->display();
	}

	public function saveRoleAction(){
		$adminCid = I('admin_cid');
		$role_ids = I('role_id');
		D($this->tableNames['user'][1])->where('admin_cid=%d', $adminCid)->delete();
		if( is_array( $role_ids ) ) {
			foreach ($role_ids as $v) {
				D($this->tableNames['user'][1])->add(array(
				'admin_cid' => $adminCid,
				'role_id' => $v
				));
			}
			$this->ajaxSuccess();
		}
		$this->ajaxError();
	}
	
	// 删除数据
	public function removePostAction() {
		$this->ajaxRemove('PartnerAdmin');
	}
	
	// 彻底删除
	public function deletePostAction() {
		$this->ajaxDelete('PartnerAdmin');
	}
	
	// 恢复用户
	public function restorePostAction() {
		$this->ajaxRestore('PartnerAdmin');
	}
	
	public function setStatusPostAction() {
		$this->ajaxSetStatus('PartnerAdmin');
	}
	
}